#include "mainwindow.h"
#include "ui_mainwindow.h"

void MainWindow::getSubStr(char src[], char dst[], int n)
{
    int c=0,i=0;
    int t=0;
    for(i=0;i<strlen(src);i++)
    {
        if(src[i]==' ')
        {
            t++;
            if(t==n)
                break;
        }
    }
    for(i=i+1;i<strlen(src);i++)
    {
        if(src[i]==' ')
            break;
        dst[c]=src[i];
        c++;
    }
    dst[c]='\0';
}


bool MainWindow::cmp_str(const char src[], const char dst[])
{
    int l=strlen(dst);
    if(l>strlen(src))
        return false;
    for(int i=0;i<l;i++)
    {
        if(src[i]!=dst[i])
            return false;
    }
    return true;
}

void MainWindow::send_str(char sendBuf[])
{
    int send_len = 0;
    int temp=0;
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB2312"));
    while(temp++<10)
    {
        send_len = tcpSocket->write(sendBuf, 1024);
//        ui->showInfo->appendPlainText(tr("send : ")+QString::number(send_len));
        if (send_len < 0) {
            qDebug() <<"try times "<<temp<< ": 发送失败！" << endl;
        }
        else
            break;
    }
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
}


bool MainWindow::send_file(char filename[],char label[])
{
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB2312"));
    detectName(filename);
    FILE *p=fopen(filename,"rb");
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
    char send_buf[1024]=" ";
    if(p==NULL)
    {
        send_str(send_buf);
        return false;
    }
    char str[1000];
    int i=0;

    int n=0;
    while(!feof(p))
    {
        n++;
        int num=fread(str,1,1000,p);
        sprintf(send_buf,"#*#%s#%d#",label,num);
        int temp=strlen(send_buf);
//        ui->showInfo->appendPlainText(tr(label)+QString::number(n)+tr(":")+QString::number(num));
        for(i=0;i<num;i++)
        {
            send_buf[temp+i]=str[i];
        }
        send_buf[temp+i]='\0';
        send_str(send_buf);
        //break;
    }
    sprintf(send_buf,"#*#end#");
    send_str(send_buf);
    fclose(p);
    return true;
}

int MainWindow::getFileInfo(char filename[])
{
    //QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB2312"));
    FILE *p=fopen(filename,"r");
    if(p==NULL)
        return -1;
    int size = 0;
    fseek(p,0,SEEK_END);
    size = ftell(p);
    fclose(p);
   // QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
    return size;
}

void MainWindow::sendFile(char srcFilename[])
{
    detectName(srcFilename);
    int size = getFileInfo(srcFilename);	//11 - 5 =6
    ui->showInfo->appendPlainText(tr("send:")+tr(srcFilename));
    char send_buf[1024];
    if(size == -1){
        sprintf(send_buf,"#*#file#-1#");
        send_str(send_buf);
        return ;
    }
    for(int i=0;i<strlen(srcFilename);i++)
    {
        if(srcFilename[i]==' ')
            srcFilename[i]='|';
    }
    sprintf(send_buf,"#*#file#%d# %s",size,srcFilename);
    send_str(send_buf);
    send_file(srcFilename,"file"); //
}



void MainWindow::sendFileList(char str[])
{
    QString substr;
    if(str[0]=='*')
        substr=QString(&str[1]);
    else
    {
        str[strlen(str)-1]='\0';
        substr=QString(str);
    }
    QDir dirPath(QDir::currentPath());
    QFileInfoList list(dirPath.entryInfoList());
    QFileInfo fileinfo;

    char send_buf[1024];
    sprintf(send_buf,"#*#flist");
    send_str(send_buf);

    for(int i=0;i<list.size();i++)
    {
        fileinfo = list.at(i);
        if(!fileinfo.isFile())
            continue;
        QString filename = fileinfo.fileName();
        if(filename.contains(substr))
        {
            QByteArray name = filename.toLocal8Bit();
            sendFile(name.data());
        }
    }

    sprintf(send_buf,"#*#endlist");
    send_str(send_buf);
}




void MainWindow::getDirInfo(char filename[])
{
    QDir dirPath(QDir::currentPath());
    QFileInfoList list(dirPath.entryInfoList());
    QFileInfo fileinfo;
    const char *str, *date;
    QByteArray temp,t2;
    FILE *p=fopen(filename,"w");
    char show[300];
    int index=0;
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB2312"));
    for(index=0;index<list.size();index++)
    {
         fileinfo=list.at(index);
         if(fileinfo.isDir())
         {
             temp = fileinfo.fileName().toLocal8Bit();
             str = temp.data();

             t2 = fileinfo.lastModified().toString("yyyy-MM-dd hh:mm:ss").toLocal8Bit();
             date = t2.data();

             fprintf(p,"%s    <DIR>  %40s \n",date,str);
         }
    }
    for(index=0;index<list.size();index++)
    {
         fileinfo=list.at(index);
         if(fileinfo.isFile())
         {
             temp = fileinfo.fileName().toLocal8Bit();
             str = temp.data();

             t2 = fileinfo.lastModified().toString("yyyy-MM-dd hh:mm:ss").toLocal8Bit();
             date = t2.data();

             fprintf(p,"%s   <FILE>  %40s  %d\n",date,str,fileinfo.size());
             sprintf(show,"%s   <FILE>  %40s  %d\n",date,str,fileinfo.size());
         }
    }
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
    fclose(p);
}

int MainWindow::getMessageSize(char str[], int &num)
{
    sscanf(&str[8],"%d",&num);
    for(int i=8;i<strlen(str);i++)
    {
        if(str[i]=='#')
            return i+1;
    }
    return 0;
}
